package com.jvyou.app.ds.service.impl;

import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.common.exception.AppException;
import com.jvyou.app.ds.client.SparkServiceClient;
import com.jvyou.app.ds.constants.SparkRole;
import com.jvyou.app.ds.domain.dto.SparkRequestDto;
import com.jvyou.app.ds.domain.dto.SparkResponseDto;
import com.jvyou.app.ds.domain.entity.QuestionType;
import com.jvyou.app.ds.domain.entity.SparkMessage;
import com.jvyou.app.ds.mapper.QuestionTypeMapper;
import com.jvyou.app.ds.service.SparkMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * @author 橘柚
 * @since 2024/12/20 12:38
 */
@Service("sparkMessageService")
public class SparkMessageServiceImpl implements SparkMessageService {

    @Resource
    private SparkServiceClient sparkServiceClient;
    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    @Override
    public String subjectOneChoice() {
        QuestionType questionType = questionTypeMapper.getRandomType();
        SparkRequestDto sparkRequestDto = new SparkRequestDto();
        sparkRequestDto.setModel("generalv3.5");
        sparkRequestDto.setUser("jvyou");
        String command = "随机给我一道驾照考试科目一的4选项选择题，类型为：" + questionType.getName() + "，返回的内容严格按照以下模板,保留中括号，确保8组中括号，所有的回答都在中括号内，最后一个中括号里面为答案，且不要有其他多余的回答，模板：[题目内容][A.A选项内容][B.B选项内容][C.C选项内容][D.D选项内容][解析内容][考点内容][A]";
        sparkRequestDto.setMessages(Collections.singletonList(new SparkMessage(SparkRole.USER, command)));
        SparkResponseDto sparkResponseDto = sparkServiceClient.chat(sparkRequestDto);
        if (sparkResponseDto == null) {
            throw new AppException(HttpCodeEnum.DATA_SERVER_CONNECT_ERROR, "数据服务器连接异常，请联系管理员启动服务器");
        }
        return sparkResponseDto.getChoices().get(0).getMessage().getContent();
    }
}
